home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 12 / Example 12.1 / effect.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-06-30  |  7.5 KB  |  225 lines

  1. #include "effect.h"
  2.  
  3. //Global Effect variables
  4. ID3DXMesh *billboardMesh = NULL;
  5. D3DMATERIAL9 whiteMtrl;
  6. SHADER effectVertexShader, effectPixelShader;
  7. D3DXHANDLE effectMatW, effectMatVP, effectVCol;
  8.  
  9. //Global Effect Textures
  10. IDirect3DTexture9* runesTexture = NULL;
  11. IDirect3DTexture9* cloudTexture = NULL;
  12.  
  13. struct SimpleVertex
  14. {
  15.     SimpleVertex(){}
  16.     SimpleVertex(D3DXVECTOR3 pos, D3DXVECTOR3 norm, D3DXVECTOR2 _uv)
  17.     {
  18.         position = pos;
  19.         normal = norm;
  20.         uv = _uv;
  21.     }
  22.  
  23.     D3DXVECTOR3 position, normal;
  24.     D3DXVECTOR2 uv;
  25.  
  26.     static const DWORD FVF;
  27. };
  28.  
  29. const DWORD SimpleVertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1;
  30.  
  31. void LoadEffectResources(IDirect3DDevice9 *m_pDevice)
  32. {
  33.     //Calculate sight mesh (a simple quad)
  34.     D3DXCreateMeshFVF(2, 4, D3DXMESH_MANAGED, SimpleVertex::FVF, m_pDevice, &billboardMesh);
  35.  
  36.     //Create 4 vertices
  37.     SimpleVertex* v = 0;
  38.     billboardMesh->LockVertexBuffer(0,(void**)&v);
  39.     v[0] = SimpleVertex(D3DXVECTOR3(-0.5f, 0.0f, 0.5f), D3DXVECTOR3(0.0f, 1.0f, 0.0f), D3DXVECTOR2(0, 0));
  40.     v[1] = SimpleVertex(D3DXVECTOR3( 0.5f, 0.0f, 0.5f), D3DXVECTOR3(0.0f, 1.0f, 0.0f), D3DXVECTOR2(1, 0));
  41.     v[2] = SimpleVertex(D3DXVECTOR3(-0.5f, 0.0f, -0.5f),D3DXVECTOR3(0.0f, 1.0f, 0.0f), D3DXVECTOR2(0, 1));
  42.     v[3] = SimpleVertex(D3DXVECTOR3( 0.5f, 0.0f, -0.5f),D3DXVECTOR3(0.0f, 1.0f, 0.0f), D3DXVECTOR2(1, 1));
  43.     billboardMesh->UnlockVertexBuffer();
  44.  
  45.     //Create 2 faces
  46.     WORD* indices = 0;
  47.     billboardMesh->LockIndexBuffer(0,(void**)&indices);    
  48.     indices[0] = 0; indices[1] = 1; indices[2] = 2;
  49.     indices[3] = 1; indices[4] = 3; indices[5] = 2;
  50.     billboardMesh->UnlockIndexBuffer();
  51.  
  52.     //Set Attributes for the 2 faces
  53.     DWORD *att = 0;
  54.     billboardMesh->LockAttributeBuffer(0,&att);
  55.     att[0] = 0; att[1] = 0;
  56.     billboardMesh->UnlockAttributeBuffer();
  57.  
  58.     //Sight MTRL
  59.     memset(&whiteMtrl, 0, sizeof(D3DMATERIAL9));
  60.     whiteMtrl.Diffuse = whiteMtrl.Ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
  61.  
  62.     //Load Shaders
  63.     effectVertexShader.Init(m_pDevice, "shaders/effect.vs", VERTEX_SHADER);
  64.     effectPixelShader.Init(m_pDevice, "shaders/effect.ps", PIXEL_SHADER);
  65.  
  66.     //Get constants
  67.     effectMatW = effectVertexShader.GetConstant("matW");
  68.     effectMatVP = effectVertexShader.GetConstant("matVP");
  69.     effectVCol = effectVertexShader.GetConstant("vertexColor");
  70.  
  71.     //Load textures
  72.     D3DXCreateTextureFromFile(m_pDevice, "textures/runes.dds", &runesTexture);
  73.     D3DXCreateTextureFromFile(m_pDevice, "textures/cloud.dds", &cloudTexture);
  74. }
  75.  
  76. void UnloadEffectResources()
  77. {
  78.     if(billboardMesh)billboardMesh->Release();
  79.     billboardMesh = NULL;
  80. }
  81.  
  82. //////////////////////////////////////////////////////////////////////////////////////////////
  83. //                                TRANSFORM                                                    //
  84. //////////////////////////////////////////////////////////////////////////////////////////////
  85.  
  86. TRANSFORM::TRANSFORM(){ m_pos = m_rot = D3DXVECTOR3(0.0f, 0.0f, 0.0f); m_sca = D3DXVECTOR3(1.0f, 1.0f, 1.0f); }
  87. TRANSFORM::TRANSFORM(D3DXVECTOR3 _pos){ m_pos = _pos; m_rot = D3DXVECTOR3(0.0f, 0.0f, 0.0f); m_sca = D3DXVECTOR3(1.0f, 1.0f, 1.0f); }
  88. TRANSFORM::TRANSFORM(D3DXVECTOR3 _pos, D3DXVECTOR3 _rot){ m_pos = _pos; m_rot = _rot; m_sca = D3DXVECTOR3(1.0f, 1.0f, 1.0f); }
  89. TRANSFORM::TRANSFORM(D3DXVECTOR3 _pos, D3DXVECTOR3 _rot, D3DXVECTOR3 _sca){ m_pos = _pos; m_rot = _rot; m_sca = _sca; }
  90.  
  91. void TRANSFORM::Init(D3DXVECTOR3 _pos){ m_pos = _pos; m_rot = D3DXVECTOR3(0.0f, 0.0f, 0.0f); m_sca = D3DXVECTOR3(1.0f, 1.0f, 1.0f); }
  92. void TRANSFORM::Init(D3DXVECTOR3 _pos, D3DXVECTOR3 _rot){ m_pos = _pos; m_rot = _rot; m_sca = D3DXVECTOR3(1.0f, 1.0f, 1.0f); }
  93. void TRANSFORM::Init(D3DXVECTOR3 _pos, D3DXVECTOR3 _rot, D3DXVECTOR3 _sca){ m_pos = _pos; m_rot = _rot; m_sca = _sca; }
  94.  
  95. D3DXMATRIX TRANSFORM::GetWorldMatrix()
  96. {
  97.     D3DXMATRIX p, r, s;
  98.     D3DXMatrixTranslation(&p, m_pos.x, m_pos.y, m_pos.z);
  99.     D3DXMatrixRotationYawPitchRoll(&r, m_rot.y, m_rot.x, m_rot.z);
  100.     D3DXMatrixScaling(&s, m_sca.x, m_sca.y, m_sca.z);
  101.     D3DXMATRIX world = s * r * p;
  102.     return world;
  103. }
  104.  
  105. //////////////////////////////////////////////////////////////////////////////////////////////
  106. //                                EFFECTS    BASE CLASS                                            //
  107. //////////////////////////////////////////////////////////////////////////////////////////////
  108.  
  109. EFFECT::EFFECT(IDirect3DDevice9 *Dev)
  110. {
  111.     m_pDevice = Dev;
  112.     m_time = 0.0f;
  113.     m_color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);    //White
  114. }
  115.  
  116. void EFFECT::PreRender()
  117. {
  118.     //Enable alpha blending
  119.     m_pDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  120.     m_pDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
  121.     m_pDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  122.     m_pDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
  123.     m_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
  124.     m_pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
  125.     m_pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE );
  126.     m_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
  127.     m_pDevice->SetRenderState(D3DRS_ZWRITEENABLE, false);
  128.     m_pDevice->SetRenderState(D3DRS_LIGHTING, false);
  129.  
  130.     //Set vertex shader variables
  131.     D3DXMATRIX view, proj;
  132.     m_pDevice->GetTransform(D3DTS_VIEW, &view);
  133.     m_pDevice->GetTransform(D3DTS_PROJECTION, &proj);
  134.     
  135.     effectVertexShader.SetMatrix(effectMatVP, view * proj);
  136.     effectVertexShader.SetVector4(effectVCol, m_color);
  137.  
  138.     //Set material
  139.     m_pDevice->SetMaterial(&whiteMtrl);
  140.  
  141.     //enable Shaders
  142.     effectVertexShader.Begin();
  143.     effectPixelShader.Begin();
  144. }
  145.  
  146. void EFFECT::PostRender()
  147. {
  148.     //Reset renderstates
  149.     m_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
  150.     m_pDevice->SetRenderState(D3DRS_ZWRITEENABLE, true);
  151.     m_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
  152.  
  153.     //Disable shaders
  154.     effectVertexShader.End();
  155.     effectPixelShader.End();
  156. }
  157.  
  158. //////////////////////////////////////////////////////////////////////////////////////////////
  159. //                                EFFECT SPELL                                                //
  160. //////////////////////////////////////////////////////////////////////////////////////////////
  161.  
  162. EFFECT_SPELL::EFFECT_SPELL(IDirect3DDevice9 *Dev, D3DXVECTOR3 _pos) : EFFECT(Dev), m_t1(_pos, D3DXVECTOR3(0.0f, 0.0f, 0.0f), D3DXVECTOR3(0.1f, 0.1f, 0.1f))
  163. {    
  164.     m_t1.m_pos.y += 1.0f;
  165.  
  166.     //Set a random m_color of the effect
  167.     m_color = D3DXVECTOR4(rand()%1000 / 1000.0f, rand()%1000 / 1000.0f, rand()%1000 / 1000.0f, 0.0f);
  168.  
  169.     //Initiate the glow transforms
  170.     for(int i=0;i<10;i++)
  171.     {
  172.         float angle = i * (D3DX_PI / 5.0f);
  173.             
  174.         m_c[i].Init(_pos + D3DXVECTOR3(cos(angle) * 0.5f, 2.5f, sin(angle) * 0.5f),
  175.                   D3DXVECTOR3(D3DX_PI * 0.5f, angle, 0.0f),
  176.                   D3DXVECTOR3(4.0f, 4.0f, 6.0f));
  177.     }
  178. }
  179.  
  180. void EFFECT_SPELL::Update(float timeDelta)
  181. {
  182.     m_time += timeDelta;
  183.  
  184.     //Update Lower spinning quad...
  185.     m_t1.m_rot.y += timeDelta;
  186.  
  187.     //Update cloud
  188.     for(int i=0;i<10;i++)
  189.         m_c[i].m_rot.y -= timeDelta;
  190.  
  191.     //Update Spinning quad scale
  192.     if(m_time < 1.5f)m_t1.m_sca += D3DXVECTOR3(timeDelta, timeDelta, timeDelta) * 4.0f;
  193.     if(m_time > 4.5f)m_t1.m_sca -= D3DXVECTOR3(timeDelta, timeDelta, timeDelta) * 4.0f;
  194.  
  195.     //Calculate alpha
  196.     m_color.w = m_t1.m_sca.x / 6.0f;
  197. }
  198.  
  199. void EFFECT_SPELL::Render()
  200. {
  201.     PreRender();
  202.  
  203.     if(billboardMesh)
  204.     {
  205.         //Spinning quad
  206.         m_pDevice->SetTexture(0, runesTexture);
  207.         effectVertexShader.SetMatrix(effectMatW, m_t1.GetWorldMatrix());
  208.         billboardMesh->DrawSubset(0);
  209.  
  210.         //Glow
  211.         m_pDevice->SetTexture(0, cloudTexture);
  212.         for(int i=0;i<10;i++)
  213.         {
  214.             effectVertexShader.SetMatrix(effectMatW, m_c[i].GetWorldMatrix());
  215.             billboardMesh->DrawSubset(0);
  216.         }
  217.     }
  218.  
  219.     PostRender();
  220. }
  221.  
  222. bool EFFECT_SPELL::isDead()
  223. {
  224.     return m_t1.m_sca.x < 0.0f;
  225. }